package com.etc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.service.impl.MessageServiceImpl;
import com.etc.service1.MessageServiceDao;
import com.etc.util.PageTool;

import java.util.*;
import com.etc.entity.*;
/**
 * Servlet implementation class MyListMessageServlet
 */
@WebServlet("/MyListMessageServlet")
public class MyListMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyListMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String search = request.getParameter("search");
		String username = request.getParameter("username");
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		MessageServiceDao ms = new MessageServiceImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<Message> list = ms.mySearch(username, search, pageindex, pagesize);
		PageTool<Message> pageMessage = new PageTool<Message>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
		request.setAttribute("list", pageMessage);
		System.out.println(search+username);
		
		request.getRequestDispatcher("html/mymessage-list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String search = request.getParameter("search");
		String username = request.getParameter("username");
		/*
		 * MessageServiceDao ms = new MessageServiceImpl(); List<Message> list =
		 * ms.mySearch(username, search); request.setAttribute("list", list);
		 * request.getRequestDispatcher("html/allmessage-list.jsp").forward(request,
		 * response);
		 */
		int pageindex=Integer.valueOf(request.getParameter("pageIndex"));
		int pagesize = 10;
		MessageServiceDao ms = new MessageServiceImpl();
		/* List<Message> list = ms.searchAllMessage(pageindex,pagesize,type); */ 
		List<Message> list = ms.mySearch(username, search, pageindex, pagesize);
		PageTool<Message> pageMessage = new PageTool<Message>();
		pageMessage.setList(list);
		pageMessage.setPageIndex(pageindex);		
		pageMessage.setPageSize(pagesize);
		Long result=ms.countAll();
		pageMessage.setTotalCount(result.intValue());
		System.out.println(username+search);
		if(search.equals("") && username.equals("")) {
			response.sendRedirect("MessageServlet?po=alltie&pageIndex=1");
		}else {
		request.setAttribute("list", pageMessage);
		request.getRequestDispatcher("html/allmessage-list.jsp").forward(request, response);
	}
	}
}
